package cn.iocoder.yudao.module.famous.doctor.controller.app.operator;

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.famous.doctor.controller.app.operator.utils.FileUtil;
import cn.iocoder.yudao.module.famous.doctor.controller.app.operator.utils.Query;
import cn.iocoder.yudao.module.famous.doctor.controller.app.operator.vo.OperatorPerformanceHead;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.form.UserProjectItemEntity;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.form.UserProjectItemMapper;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.operator.OperatorPerformanceHeadMapper;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import dm.jdbc.util.StringUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 后台-绩效管理
 *
 * @author wyx
 * @since 2021-08-17
 */
@Validated
@RestController
@RequestMapping("/manager/operator/performance")
@Tag(name = "运营商-后台管理-绩效管理")
public class ManagerOperatorPerformanceController
{

	@PostMapping("/save")
	@Operation(summary ="绩效管理 - 保存")
	public CommonResult<OperatorPerformanceHead> save(@RequestBody OperatorPerformanceHead operatorPerformanceHead) {
		operatorPerformanceHead.setImg(FileUtil.getShortFileName(operatorPerformanceHead.getImg()));
		if (operatorPerformanceHead.getId() == null) {
			Long userId = getLoginUserId();
			MemberUserDO user = userService.getUser(userId);
			operatorPerformanceHead.setOperatorName(user.getNickname());
			operatorPerformanceHead.setOperatorId(userId);
			operatorPerformanceHead.setKey(IdUtil.fastSimpleUUID());
			operatorPerformanceHeadMapper.insert(operatorPerformanceHead);
		}
		else {

			operatorPerformanceHeadMapper.updateById(operatorPerformanceHead);
		}
		return success(operatorPerformanceHead);
	}


	@GetMapping("/list")
	@Operation(summary ="绩效管理 - 列表")
	@Parameter(name = "pageNum", description = "当前页码", required = false, example = "1")
	@Parameter(name = "pageSize", description = "每页显示记录数", required = false, example = "1")
	@Parameter(name = "name", description = "业务名称", required = false, example = "1")
	public CommonResult<IPage<OperatorPerformanceHead>> list(@RequestParam(required = false) Map<String, Object> params) {
		String name = params.containsKey("name") ? params.get("name").toString().trim() : null;
		String isTemplate = params.containsKey("isTemplate") ? params.get("isTemplate").toString().trim() : null;
		IPage<OperatorPerformanceHead> page = operatorPerformanceHeadMapper.selectPage(new Query().getPage(params),new QueryWrapper<OperatorPerformanceHead>().lambda()
				.like(StringUtil.isNotEmpty(name), OperatorPerformanceHead::getName, name)
				.eq(OperatorPerformanceHead::getIsDel, 0)
				.eq(OperatorPerformanceHead::getOperatorId,getLoginUserId())
		);
		return success(page);
	}

	@DeleteMapping("/delete")
	@Operation(summary ="绩效管理 - 删除")
	public CommonResult delete(Long id) {
		operatorPerformanceHeadMapper.deleteById(id);
		return success(id);
	}

	@GetMapping("/view")
	@Operation(summary ="绩效管理 - 详情")
	public CommonResult<OperatorPerformanceHead> view(Long id) {
		OperatorPerformanceHead operatorPerformanceHead = operatorPerformanceHeadMapper.selectById(id);
		return success(operatorPerformanceHead);
	}

	@GetMapping("/projectItemList")
	@Operation(summary ="绩效-表单项")
	public CommonResult<List<UserProjectItemEntity>> projectItemList(String key) {
		List<UserProjectItemEntity> userProjectItemEntities = userProjectItemEntityMapper.selectList(new QueryWrapper<UserProjectItemEntity>().lambda()
				.eq(UserProjectItemEntity::getProjectKey, key).orderByAsc(UserProjectItemEntity::getSort));
		return success(userProjectItemEntities);
	}
	
	@Autowired
	private OperatorPerformanceHeadMapper operatorPerformanceHeadMapper;
	@Autowired
	private UserProjectItemMapper userProjectItemEntityMapper;
	@Autowired
	private MemberUserService userService;
}
